package customize

import (
	"gitcode.com/G-YT/baoyou/server/global"
	"gitcode.com/G-YT/baoyou/server/model/common/request"
	"gitcode.com/G-YT/baoyou/server/model/customize"
	customizeReq "gitcode.com/G-YT/baoyou/server/model/customize/request"
	"gorm.io/gorm"
)

type EfficacyService struct{}

// GetEfficacyList 获取功效列表
// Author [G-YT](https://gitcode.com/G-YT)
func (efficacyService *EfficacyService) GetEfficacyList() (efficacy []customize.Efficacy, err error) {
	err = global.GVA_DB.Find(&efficacy).Error
	return
}

// CreateEfficacy 创建功效记录
// Author [G-YT](https://gitcode.com/G-YT)
func (efficacyService *EfficacyService) CreateEfficacy(efficacy *customize.Efficacy) (err error) {
	err = global.GVA_DB.Create(efficacy).Error
	return err
}

// DeleteEfficacy 删除功效记录
// Author [G-YT](https://gitcode.com/G-YT)
func (efficacyService *EfficacyService) DeleteEfficacy(efficacy customize.Efficacy) (err error) {
	err = global.GVA_DB.Delete(&efficacy).Error
	return err
}

// DeleteEfficacyByIds 批量删除功效记录
// Author [G-YT](https://gitcode.com/G-YT)
func (efficacyService *EfficacyService) DeleteEfficacyByIds(ids request.IdsReq) (err error) {
	err = global.GVA_DB.Delete(&[]customize.Efficacy{}, "id in ?", ids.Ids).Error
	return err
}

// UpdateEfficacy 更新功效记录
// Author [G-YT](https://gitcode.com/G-YT)
func (efficacyService *EfficacyService) UpdateEfficacy(efficacy customize.Efficacy) (err error) {
	err = global.GVA_DB.Save(&efficacy).Error
	return err
}

// GetEfficacy 根据id获取功效记录
// Author [G-YT](https://gitcode.com/G-YT)
func (efficacyService *EfficacyService) GetEfficacy(id uint) (efficacy customize.Efficacy, err error) {
	err = global.GVA_DB.Where("id = ?", id).First(&efficacy).Error
	return
}

// GetEfficacyInfoList 分页获取功效记录
// Author [G-YT](https://gitcode.com/G-YT)
func (efficacyService *EfficacyService) GetEfficacyInfoList(info customizeReq.EfficacySearch) (list []customize.Efficacy, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&customize.Efficacy{})
	var efficacyList []customize.Efficacy
	// 如果有条件搜索 下方会自动创建搜索语句
	err = db.Count(&total).Error
	if err != nil {
		return
	}

	if limit != 0 {
		db = db.Limit(limit).Offset(offset)
	}

	err = db.Find(&efficacyList).Error
	return efficacyList, total, err
}

// SetEfficacyArticle 设置功效文章关系
// Author [G-YT](https://gitcode.com/G-YT)
func (efficacyService *EfficacyService) SetEfficacyArticle(req customizeReq.EfficacyArticleReq) (err error) {
	return global.GVA_DB.Transaction(func(tx *gorm.DB) error {
		if err := tx.Where("efficacy_id = ?", req.EfficacyID).Delete(&customize.EfficacyArticle{}).Error; err != nil {
			return err
		}
		var efficacyIDArticles []customize.EfficacyArticle
		for _, richText := range req.RichTexts {
			efficacyIDArticles = append(efficacyIDArticles, customize.EfficacyArticle{
				EfficacyID: req.EfficacyID,
				ArticleID:  richText,
				Type:       "richText",
			})
		}
		for _, audio := range req.Audios {
			efficacyIDArticles = append(efficacyIDArticles, customize.EfficacyArticle{
				EfficacyID: req.EfficacyID,
				ArticleID:  audio,
				Type:       "audio",
			})
		}
		for _, video := range req.Videos {
			efficacyIDArticles = append(efficacyIDArticles, customize.EfficacyArticle{
				EfficacyID: req.EfficacyID,
				ArticleID:  video,
				Type:       "video",
			})
		}
		if len(efficacyIDArticles) == 0 {
			return nil
		}
		return tx.Create(&efficacyIDArticles).Error
	})
}

// GetEfficacyArticle 获取功效文章关系
// Author [G-YT](https://gitcode.com/G-YT)
func (efficacyService *EfficacyService) GetEfficacyArticle(efficacyID string) (DiseaseGraphic []customize.EfficacyArticle, err error) {
	err = global.GVA_DB.Where("efficacy_id = ?", efficacyID).Find(&DiseaseGraphic).Error
	return
}
